package src.Packets;

import src.Players.*;
import src.Main;
import src.World.*;

public class Frames
{
    public int[] sidebars = {2423,3917,638,3213,1644,5608,12855,-1,5065,5715,2449,904,147,962};

    public void loginFrame(Player p)
    {
        if(p == null || p.outStream == null)
        {
            return;
        }
        p.outStream.createFrame(249);
        p.outStream.writeByteA(1);
        p.outStream.writeWordBigEndianA(p.playerIndex);
        p.outStream.createFrame(107);
        p.outStream.createFrame(68); //Reset every button on the client.
    }

    public void setSidebar(Player p, int interfaceId, int idx)
    {
        if(p == null || p.outStream == null)
        {
            return;
        }
        p.outStream.createFrame(71);
        p.outStream.writeWord(interfaceId);
        p.outStream.writeByteA(idx);
    }

    public void sendSkillLvl(Player p, int lvlId)
    {
        if(p == null || p.outStream == null)
        {
            return;
        }
        if(lvlId == 0)
        {
            setText126(p, p.skillLvl[0] + "", 4004);
        }
        else if(lvlId == 1)
        {
            setText126(p, p.skillLvl[1] + "", 4008);
        }
        else if(lvlId == 2)
        {
            setText126(p, p.skillLvl[2] + "", 4006);
        }
        else if(lvlId == 3)
        {
            setText126(p, p.skillLvl[3] + "", 4016);
        }
        else if(lvlId == 4)
        {
            setText126(p, p.skillLvl[4] + "", 4010);
        }
        else if(lvlId == 5)
        {
            setText126(p, p.skillLvl[5] + "", 4012);
        }
        else if(lvlId == 6)
        {
            setText126(p, p.skillLvl[6] + "", 4014);
        }
        else if(lvlId == 7)
        {
            setText126(p, p.skillLvl[7] + "", 4034);
        }
        else if(lvlId == 8)
        {
            setText126(p, p.skillLvl[8] + "", 4038);
        }
        else if(lvlId == 9)
        {
            setText126(p, p.skillLvl[9] + "", 4026);
        }
        else if(lvlId == 10)
        {
            setText126(p, p.skillLvl[10] + "", 4032);
        }
        else if(lvlId == 11)
        {
            setText126(p, p.skillLvl[11] + "", 4036);
        }
        else if(lvlId == 12)
        {
            setText126(p, p.skillLvl[12] + "", 4024);
        }
        else if(lvlId == 13)
        {
            setText126(p, p.skillLvl[13] + "", 4030);
        }
        else if(lvlId == 14)
        {
            setText126(p, p.skillLvl[14] + "", 4028);
        }
        else if(lvlId == 15)
        {
            setText126(p, p.skillLvl[15] + "", 4020);
        }
        else if(lvlId == 16)
        {
            setText126(p, p.skillLvl[16] + "", 4018);
        }
        else if(lvlId == 17)
        {
            setText126(p, p.skillLvl[17] + "", 4022);
        }
        else if(lvlId == 18)
        {
            setText126(p, p.skillLvl[18] + "", 12166);
        }
        else if(lvlId == 19)
        {
            setText126(p, p.skillLvl[19] + "", 13926);
        }
        else if(lvlId == 20)
        {
            setText126(p, p.skillLvl[20] + "", 4152);
        }
        p.outStream.createFrame(134);
        p.outStream.writeByte(lvlId);
        p.outStream.writeDWord_v1(p.skillXP[lvlId]);
        p.outStream.writeByte(p.skillLvl[lvlId]);
    }

    public void setText126(Player p, String text, int interfaceId)
    {
        if(p == null || p.outStream == null)
        {
            return;
        }
        p.outStream.createFrameVarSizeWord(126);
        p.outStream.writeString(text);
        p.outStream.writeWordA(interfaceId);
        p.outStream.endFrameVarSizeWord();
    }

    public void logout(Player p)
    {
        if(p == null || p.outStream == null)
        {
            return;
        }
        p.outStream.createFrame(109);
        p.disconnected[0] = p.disconnected[1] = true;
    }

    public void resetItems(Player p, int interfaceId)
    {
        if(p == null || p.outStream == null)
        {
            return;
        }
        p.outStream.createFrameVarSizeWord(53);
        p.outStream.writeWord(interfaceId);
        p.outStream.writeWord(p.items.length);
        for(int i = 0; i < p.items.length; i++)
        {
            if(p.itemsN[i] > 254)
            {
                p.outStream.writeByte(255);
                p.outStream.writeDWord_v2(p.itemsN[i]);
            }
            else
            {
                p.outStream.writeByte(p.itemsN[i]);
            }
            p.outStream.writeWordBigEndianA(p.items[i]);
        }
        p.outStream.endFrameVarSizeWord();
    }

    public void resetTItems(Player p, int interfaceId) {
	PlayerItems pi = new PlayerItems();
	Items items = new Items();
	p.outStream.createFrameVarSizeWord(53);
	p.outStream.writeWord(interfaceId);
	p.outStream.writeWord(pi.playerTItems.length);
	for (int i = 0; i < pi.playerTItems.length; i++) {
		if (pi.playerTItemsN[i] > 254) {
			p.outStream.writeByte(255);
			p.outStream.writeDWord_v2(pi.playerTItemsN[i]);
		} else {
			p.outStream.writeByte(pi.playerTItemsN[i]);
		}
		if (pi.playerTItems[i] > items.maxItemAmount || pi.playerTItems[i] < 0) {
			pi.playerTItems[i] = items.maxItemAmount;
		}
		p.outStream.writeWordBigEndianA(pi.playerTItems[i]);
	}
	p.outStream.endFrameVarSizeWord();
    }

    public void setEquipment(Player p, int wearId, int amount, int targetSlot)
    {
        if(p == null || p.outStream == null)
        {
            return;
        }
        p.outStream.createFrameVarSizeWord(34);
        p.outStream.writeWord(1688);
        p.outStream.writeByte(targetSlot);
        p.outStream.writeWord((wearId + 1));
        if (amount > 254)
        {
            p.outStream.writeByte(255);
            p.outStream.writeDWord(amount);
        }
        else
        {
            p.outStream.writeByte(amount);
        }
        p.outStream.endFrameVarSizeWord();
        p.calculateEquipmentBonus();
        PlayerWeapon pw = new PlayerWeapon(p);
        pw.sendWeapon();
        pw = null;
    }

    public void removeEquipment(Player p, int wearId, int index)
    {
        if(p == null || wearId <= 0 || index < 0 || index > 12 || p.outStream == null)
        {
            return;
        }
        PlayerItems pi = new PlayerItems();
        if(!pi.addItem(p, p.equipment[index], p.equipmentN[index]))
        {
            sendMessage(p, "Not enough space in your inventory.");
            return;
        }
        p.equipment[index] = -1;
        p.equipmentN[index] = 0;
        p.outStream.createFrame(34);
        p.outStream.writeWord(6);
        p.outStream.writeWord(1688);
        p.outStream.writeByte(index);
        p.outStream.writeWord(0);
        p.outStream.writeByte(0);
        p.appearanceUpdateRequired = true;
        p.updateRequired = true;
        p.calculateEquipmentBonus();
        pi = null;
        PlayerWeapon pw = new PlayerWeapon(p);
        pw.sendWeapon();
        pw = null;
    }

    public void sendMessage(Player p, String s)
    {
        if(p == null || s == null || p.outStream == null)
        {
            return;
        }
        p.outStream.createFrameVarSize(253);
        p.outStream.writeString(s);
        p.outStream.endFrameVarSize();
    }

    public void resetBank(Player p)
    {
        if(p == null || p.outStream == null)
        {
            return;
        }
        p.outStream.createFrameVarSizeWord(53);
        p.outStream.writeWord(5382);
        p.outStream.writeWord(Main.m.ge.item.maxBankSize);
        for(int i = 0; i < Main.m.ge.item.maxBankSize; i++)
        {
            if(p.bankItemsN[i] > 254)
            {
                p.outStream.writeByte(255);
                p.outStream.writeDWord_v2(p.bankItemsN[i]);
            }
            else
            {
                p.outStream.writeByte(p.bankItemsN[i]);
            }
            if(p.bankItemsN[i] < 1)
            {
                p.bankItems[i] = 0;
            }
            p.outStream.writeWordBigEndianA(p.bankItems[i]);
        }
        p.outStream.endFrameVarSizeWord();
    }

    public void removeAllWindows(Player p)
    {
        if(p == null || p.outStream == null)
        {
            return;
        }
        p.outStream.createFrame(219);
        p.interfaceId = -1;
        p.myShopId = 0;
        p.dialogue = -1;
    }

    public void showInterface(Player p, int mainFrame, int sidebar)
    {
        if(p == null || p.outStream == null)
        {
            return;
        }
        p.outStream.createFrame(248);
        p.outStream.writeWordA(mainFrame);
        p.outStream.writeWord(sidebar);
        p.interfaceId = mainFrame;
    }

    public void showInterface(Player p, int mainFrame)
    {
        if(p == null || p.outStream == null)
        {
	    System.out.println("sf error");
            return;
        }
        p.outStream.createFrame(97);
        p.outStream.writeWord(mainFrame);
        p.interfaceId = mainFrame;
    }

    public void chatboxInterface(Player p, int frame)
    {
        if(p == null || p.outStream == null)
        {
            return;
        }
        p.outStream.createFrame(164);
        p.outStream.writeWordBigEndian_dup(frame);
        p.interfaceId = frame;
    }

    public void removeGroundItem(int itemId, int itemX, int itemY, int itemH, Player p)
    {
        if(p == null || p.outStream == null || itemId < 0)
        {
            return;
        }
        if(p.isOnline && !p.disconnected[0])
        {
            if(Main.m.ge.getDistance(itemX, itemY, p.absX, p.absY) <= 60 && p.heightLevel == itemH)
            {
                sendCoords(p, (itemX - 8 * p.mapRegionX), (itemY - 8 * p.mapRegionY));
                p.outStream.createFrame(156);
                p.outStream.writeByteS(0);
                p.outStream.writeWord(itemId);
            }
        }
    }

    public void createGroundItem(int itemId, int itemAmt, int itemX, int itemY, int itemH, Player p)
    {
        if(p == null || p.outStream == null || itemId < 0)
        {
            return;
        }
        if(p.isOnline && !p.disconnected[0])
        {
            if(Main.m.ge.getDistance(itemX, itemY, p.absX, p.absY) <= 60 && p.heightLevel == itemH)
            {
                sendCoords(p, (itemX - 8 * p.mapRegionX), (itemY - 8 * p.mapRegionY));
                p.outStream.createFrame(44);
                p.outStream.writeWordBigEndianA(itemId);
                p.outStream.writeWord(itemAmt);
                p.outStream.writeByte(0);
            }
        }
    }

    public void resetShop(Player p, int shopId)
    {
        if(p == null || p.outStream == null || shopId < 0 || Main.m.ge.shops.shopLists[shopId] == null)
        {
            return;
        }
        ShopList sl = Main.m.ge.shops.shopLists[shopId];
        int totalItems = 0;
        for(int i = 0; i < Main.m.ge.shops.maxShopItems; i++)
        {
            if(sl.getShopItems()[i] > 0)
            {
                totalItems++;
            }
        }
        if(totalItems > Main.m.ge.shops.maxShopItems)
        {
            totalItems = Main.m.ge.shops.maxShopItems;
        }
        p.outStream.createFrameVarSizeWord(53);
        p.outStream.writeWord(3900);
        p.outStream.writeWord(totalItems);
        int totalCount = 0;
        for(int i = 0; i < sl.getShopItems().length; i++)
        {
            if(sl.getShopItems()[i] > 0 || i <= sl.shopItemsStandard)
            {
                if(sl.getShopItemsN()[i] > 254)
                {
                    p.outStream.writeByte(255);
                    p.outStream.writeDWord_v2(sl.getShopItemsN()[i]);
                }
                else
                {
                    p.outStream.writeByte(sl.getShopItemsN()[i]);
                }
                p.outStream.writeWordBigEndianA(sl.getShopItems()[i]);
                totalCount++;
            }
            if(totalCount > totalItems)
            {
                break;
            }
        }
        p.outStream.endFrameVarSizeWord();
    }

    public void sendCoords(Player p, int x, int y)
    {
        if(p == null || p.outStream == null)
        {
            return;
        }
        p.outStream.createFrame(85);
        p.outStream.writeByteC(y);
        p.outStream.writeByteC(x);
    }

    public void createProjectile(Player p, int casterY, int casterX, int offsetY, int offsetX, int gfxMoving, int startHeight, int endHeight, int speed, int atkIndex)
    {
        if(p == null || p.outStream == null)
        {
            return;
        }
        sendCoords(p, (casterX - (p.mapRegionX * 8)) - 3, (casterY - (p.mapRegionY * 8)) - 2);
        p.outStream.createFrame(117);
        p.outStream.writeByte(50);
        p.outStream.writeByte(offsetY);
        p.outStream.writeByte(offsetX);
        p.outStream.writeWord(atkIndex);
        p.outStream.writeWord(gfxMoving);
        p.outStream.writeByte(startHeight);
        p.outStream.writeByte(endHeight);
        p.outStream.writeWord(51);
        p.outStream.writeWord(speed);
        p.outStream.writeByte(16);
        p.outStream.writeByte(64);
    }

    public void stillGFX(Player p, int id, int x, int y, int height, int delay)
    {
        if(p == null || p.outStream == null)
        {
            return;
        }
        sendCoords(p, x - (p.mapRegionX * 8), y - (p.mapRegionY * 8));
        p.outStream.createFrame(4);
        p.outStream.writeByte(0);
        p.outStream.writeWord(id);
        p.outStream.writeByte(height);
        p.outStream.writeWord(delay);
    }

    public void setInterfaceItem(Player p, int interfaceId, int size, int item)
    {
        if(p == null || p.outStream == null)
        {
            return;
        }
        p.outStream.createFrame(246);
        p.outStream.writeWordBigEndian(interfaceId);
        p.outStream.writeWord(size);
        p.outStream.writeWord(item);
    }

    public void setPlayerOption(Player p, String option, int slot)
    {
        if(p == null || p.outStream == null)
        {
            return;
        }
        p.outStream.createFrameVarSize(104);
        p.outStream.writeByteC(slot);
        p.outStream.writeByteA(1);
        p.outStream.writeString(option);
        p.outStream.endFrameVarSize();
    }
}